package com.zscat.mallplus.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.active.service.ISysStoreService;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.cms.entity.CmsSubject;
import com.zscat.mallplus.cms.service.ICmsSubjectService;
import com.zscat.mallplus.pms.entity.PmsProduct;
import com.zscat.mallplus.pms.entity.PmsSkuStock;
import com.zscat.mallplus.pms.service.IPmsProductService;
import com.zscat.mallplus.pms.service.IPmsSkuStockService;
import com.zscat.mallplus.sms.entity.SmsCoupon;
import com.zscat.mallplus.sms.entity.SmsHomeAdvertise;
import com.zscat.mallplus.sms.service.ISmsCouponService;
import com.zscat.mallplus.sms.service.ISmsHomeAdvertiseService;
import com.zscat.mallplus.sys.entity.SysStore;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 商城首页
 *
 * @Auther: rwz
 * @Date: 2020/09/17/1:51 下午
 * @Description:
 */
@Slf4j
@RestController
@Api(tags = "MarketHomeController", description = "首页")
@RequestMapping("/api/market/home")
public class MarketHomeController {

    @Resource
    private ISmsCouponService couponService;

    @Autowired
    private ISmsHomeAdvertiseService advertiseService;

    @Autowired
    private IPmsProductService productService;

    @Resource
    private ISysStoreService storeService;

    @Resource
    private IPmsSkuStockService skuStockService;

    /**
     * 首页广告轮播图展示
     *
     * @param type
     * @param storeId
     * @return
     */
    @IgnoreAuth
    @ApiOperation("广告轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "轮播位置：1->小程序首页轮播；2->商城首页轮播", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "storeId", value = "店铺ID", required = false, dataType = "int", paramType = "query")
    })
    @SysLog(MODULE = "home", REMARK = "advertList")
    @GetMapping("advertList")
    public Object advertList(@RequestParam(value = "type", required = false) Integer type,
                             @RequestParam(value = "storeId", required = false) Integer storeId) {
        SmsHomeAdvertise advertise = new SmsHomeAdvertise();
        advertise.setStatus(1);
        if (ValidatorUtils.notEmpty(storeId)) {
            advertise.setStoreId(storeId);
        }
        if (ValidatorUtils.notEmpty(type)) {
            advertise.setType(type);
        }
        return new CommonResult().success(advertiseService.list(new QueryWrapper<>(advertise).lt("start_time", new Date()).gt("end_time", new Date())));
    }

    /**
     * 首页秒杀活动展示
     *
     * @param entity
     * @param pageNum
     * @param pageSize
     * @return
     */
    @IgnoreAuth
    @ApiOperation("秒杀活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "店铺ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "优惠卷类型；0->全场赠券；1->会员赠券；2->购物赠券；3->注册赠券", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "platform", value = "使用平台：0->全部；1->移动；2->PC", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "数量", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "amount", value = "金额", required = false, dataType = "BigDecimal", paramType = "query"),
            @ApiImplicitParam(name = "perLimit", value = "每人限领张数", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "minPoint", value = "使用门槛：0->表示无门槛", required = false, dataType = "BigDecimal", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "useType", value = "使用类型：0->全场通用；1->指定分类；2->指定商品", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "note", value = "备注", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "publishCount", value = "发行数量", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "useCount", value = "已使用数量", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "receiveCount", value = "领取数量", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "enableTime", value = "可以领取的日期", required = false, dataType = "Date", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "优惠码", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "memberLevel", value = "可领取的会员类型：0->无限时", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false, dataType = "int", paramType = "query", defaultValue = "10")
    })
    @RequestMapping(value = "secondSaleList", method = RequestMethod.GET)
    public Object secondSaleList(SmsCoupon entity,
                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        return new CommonResult().success(couponService.page(new Page<SmsCoupon>(pageNum, pageSize), new QueryWrapper<>(entity)));
    }

    @IgnoreAuth
    @ApiOperation("拼团活动")
    @SysLog(MODULE = "home", REMARK = "拼团活动")
    @RequestMapping(value = "groupSaleList", method = RequestMethod.GET)
    public Object groupSaleList() {
        return new CommonResult().success(productService.selectGroupCommodity());
    }

    /**
     * 首页特价商品展示
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @IgnoreAuth
    @ApiOperation("特价商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "科技馆ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false, dataType = "int", paramType = "query", defaultValue = "10")
    })
    @SysLog(MODULE = "home", REMARK = "特价商品")
    @RequestMapping(value = "specialOfferList", method = RequestMethod.GET)
    public Object specialOfferList(@RequestParam(value = "storeId", required = false) Integer storeId,
                                   @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PmsProduct product = new PmsProduct();
        product.setIsSpecial(1);
        product.setStoreId(storeId);
        product.setVerifyStatus(1);
        product.setPublishStatus(1);
        product.setDeleteStatus(1);
        IPage<PmsProduct> page = productService.page(new Page<PmsProduct>(pageNum, pageSize), new QueryWrapper<>(product).orderByDesc("create_time"));
        List<PmsProduct> productList = new ArrayList<PmsProduct>();
        for (PmsProduct pmsProduct : page.getRecords()) {
            // 根据商品ID查询商品库存
            List<PmsSkuStock> skuStockList = skuStockService.list(new QueryWrapper<PmsSkuStock>().eq("product_id", pmsProduct.getId()));
            pmsProduct.setSkuStockList(skuStockList);
            productList.add(pmsProduct);
        }
        page.setRecords(productList);
        return new CommonResult().success(page);
    }

    @IgnoreAuth
    @ApiOperation(value = "猜你喜欢")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "科技馆ID", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false, dataType = "int", paramType = "query", defaultValue = "10")
    })
    @SysLog(MODULE = "home", REMARK = "猜你喜欢")
    @RequestMapping(value = "favouriteList", method = RequestMethod.GET)
    public Object favouriteList(@RequestParam(value = "storeId", required = false) Integer storeId,
                                @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        PmsProduct product = new PmsProduct();
        product.setIsPrefer(1);
        if (ValidatorUtils.notEmpty(storeId)) {
            product.setStoreId(storeId);
        }
        product.setVerifyStatus(1);
        product.setPublishStatus(1);
        product.setDeleteStatus(1);
        IPage<PmsProduct> page = productService.page(new Page<PmsProduct>(pageNum, pageSize), new QueryWrapper<>(product).orderByDesc("create_time"));
        List<PmsProduct> productList = new ArrayList<PmsProduct>();
        for (PmsProduct pmsProduct : page.getRecords()) {
            // 根据商品ID查询商品库存
            List<PmsSkuStock> skuStockList = skuStockService.list(new QueryWrapper<PmsSkuStock>().eq("product_id", pmsProduct.getId()));
            pmsProduct.setSkuStockList(skuStockList);
            productList.add(pmsProduct);
        }
        page.setRecords(productList);
        return new CommonResult().success(page);
    }

    @ApiOperation(value = "根据城市定位获取当前城市所有科技馆")
    @ApiImplicitParam(name = "city", value = "当前城市", required = true, dataType = "strign", paramType = "query")
    @SysLog(MODULE = "home", REMARK = "根据城市定位获取当前城市所有科技馆")
    @GetMapping(value = "getStoreByCityLocation")
    public Object getStoreByCityLocation(@RequestParam(value = "city", required = true) String city) {
        if (ValidatorUtils.empty(city)) {
            return new CommonResult().failed("城市定位不能为空");
        }

        return storeService.list(new QueryWrapper<SysStore>().eq("address_city", city));
    }
}
